/**
 * 金额输入框失去焦点时的影响函数
 * @param moneyId，金额输入框的ID
 * @param tipId，显示提示信息的“P”元素的ID
 * @return
 */
function moneyOnBlur(moneyId, tipId) {
	var validator = new Assistant.validator(tipId);
	validator.checkMoney(moneyId, "金额非法");
	validator.checkZero(moneyId, "金额不能为零");
}

/**
 * 表格
 */
$(function() {
	var onRowDblClick = function(rowId, iRow, iCol, e) {
		var myGrid = $("#tally_grid");
		var row = myGrid.getRowData(rowId);

		var id = rowId;
		var dealDate = row['dealDate'];
		var inoutItemId = row['inoutItemId'];
		var inoutItemType = row['inoutItemType'];
		var inAccountId = row['inAccountId'];
		var outAccountId = row['outAccountId'];
		var money = row['money'];
		var moneyType = row['moneyType'];
		var relationUserId = row['relationUserId'];
		var memo = row['memo'];

		if (inoutItemType == "0") {// 支出
			$("#out_id").val(id);
			$("#out_dealDate").val(dealDate);
			$("#out_inoutItem").val(inoutItemId);
			$("#out_money").val(money);
			$("#out_account").val(outAccountId);
			$("#out_relationUser").val(relationUserId);
			$("#out_memo").val(memo);

			$('#dialog_out').dialog('open');
			$('#dialog_out').dialog('option', 'title', '修改支出');
		} else if (inoutItemType == "1") {// 收入
			$("#in_id").val(id);
			$("#in_dealDate").val(dealDate);
			$("#in_inoutItem").val(inoutItemId);
			$("#in_money").val(money);
			$("#in_account").val(inAccountId);
			$("#in_relationUser").val(relationUserId);
			$("#in_memo").val(memo);

			$('#dialog_in').dialog('open');
			$('#dialog_in').dialog('option', 'title', '修改收入');
		} else if (inoutItemType == "2") {// 存取款
			$("#access_id").val(id);
			$("#access_dealDate").val(dealDate);
			$("#access_inoutItem").val(inoutItemId);
			$("#access_inoutItem_"+inoutItemId).attr('checked','checked');
			$("#access_money").val(money);
			if (inoutItemId == '1') {// 取款
				$("#access_cash_account").val(inAccountId);
				$("#access_bank_account").val(outAccountId);
			} else if (inoutItemId == '2') {// 存款
				$("#access_cash_account").val(outAccountId);
				$("#access_bank_account").val(inAccountId);
			}
			$("#access_relationUser").val(relationUserId);
			$("#access_memo").val(memo);

			$('#dialog_access').dialog('open');
			$('#dialog_access').dialog('option', 'title', '修改存取款');
		} else if (inoutItemType == "3") {// 转账
			$("#transfer_id").val(id);
			$("#transfer_dealDate").val(dealDate);
			$("#transfer_inoutItem").val(inoutItemId);
			$("#transfer_inoutItem_"+inoutItemId).attr('checked','checked');
			$("#transfer_money").val(money);
			$("#transfer_out_account").val(outAccountId);
			$("#transfer_in_account").val(inAccountId);
			$("#transfer_relationUser").val(relationUserId);
			$("#transfer_memo").val(memo);
 
			$('#dialog_transfer').dialog('open');
			$('#dialog_transfer').dialog('option', 'title', '修改转账');
		} else {// 默认

		}
	}

	var moneyFormatter = function(cellValue, options, rowObject) {
		var money = Assistant.tools.formatCurrency(cellValue);
		var inoutItemType = rowObject[4];
		var newMoney;

		if (inoutItemType == "0") {// 支出
			newMoney = "<font color='#FF0000'>" + money + "</font>";
		} else if (inoutItemType == "1") {// 收入
			newMoney = "<font color='#0000FF'>" + money + "</font>";
		} else if (inoutItemType == "2") {// 存取款
			newMoney = "<font color='#666666'>" + money + "</font>";
		} else if (inoutItemType == "3") {// 转账
			newMoney = "<font color='#7F0000'>" + money + "</font>";
		} else {// 默认
			newMoney = "<font color='#000000'>" + money + "</font>";
		}

		return newMoney;
	};
	var moneyUnFormatter = function(cellValue, options, rowObject) {
		return Assistant.tools.unFormatCurrency(cellValue);
	};

	var inoutItemFormatter = function(cellValue, options, rowObject) {
		var inoutItemType = rowObject[4];
		var newInoutItem;

		if (inoutItemType == "0") {// 支出
			newInoutItem = "<font color='#FF0000'>" + cellValue + "</font>";
		} else if (inoutItemType == "1") {// 收入
			newInoutItem = "<font color='#0000FF'>" + cellValue + "</font>";
		} else if (inoutItemType == "2") {// 存取款
			newInoutItem = "<font color='#666666'>" + cellValue + "</font>";
		} else if (inoutItemType == "3") {// 转账
			newInoutItem = "<font color='#7F0000'>" + cellValue + "</font>";
		} else {// 默认
			newInoutItem = "<font color='#000000'>" + cellValue + "</font>";
		}

		return newInoutItem;
	}

	$("#tally_grid").jqGrid(
			{
				url : webRoot + '/hcm/tally.t?m=get',
				datatype : "json",
				colNames : [ 'ID', '交易日期', '收支项目代码', '收支项目', '收支项目类型',
				        '流出账户代码', '流出账户', '流入账户代码', '流入账户', '金额', '币种', '备注',
						'关联人代码', '关联人', '记账人' ],
				colModel : [ {
					name : 'id',
					index : 'id',
					width : 1,
					hidden : true
				}, {
					name : 'dealDate',
					index : 'dealDate',
					width : 80,
					align : "center",
					sortable : true
				}, {
					name : 'inoutItemId',
					index : 'inoutItemId',
					width : 1,
					hidden : true
				}, {
					name : 'inoutItemName',
					index : 'inoutItemName',
					width : 80,
					align : "left",
					sortable : true,
					formatter : inoutItemFormatter
				}, {
					name : 'inoutItemType',
					index : 'inoutItemType',
					width : 1,
					hidden : true
				}, {
					name : 'outAccountId',
					index : 'outAccountId',
					width : 1,
					hidden : true
				}, {
					name : 'outAccountName',
					index : 'outAccountName',
					width : 80,
					sortable : false,
					align : "left"
				}, {
					name : 'inAccountId',
					index : 'inAccountId',
					width : 1,
					hidden : true
				}, {
					name : 'inAccountName',
					index : 'inAccountName',
					width : 80,
					sortable : false,
					align : "left"
				}, {
					name : 'money',
					index : 'money',
					width : 80,
					sortable : true,
					align : "right",
					formatter : moneyFormatter,
					unformat : moneyUnFormatter
				}, {
					name : 'moneyType',
					index : 'moneyType',
					width : 40,
					align : "center",
					sortable : false
				}, {
					name : 'memo',
					index : 'memo',
					width : 200,
					align : "left",
					sortable : false
				}, {
					name : 'relationUserId',
					index : 'relationUserId',
					width : 1,
					hidden : true
				}, {
					name : 'relationUserName',
					index : 'relationUserName',
					width : 80,
					align : "left",
					sortable : true
				}, {
					name : 'logUserName',
					index : 'logUserName',
					width : 80,
					align : "left",
					sortable : false
				} ],
				rowNum : Assistant.global.rowNum,
				rowList : Assistant.global.rowList,
				imgpath : gridimgpath,
				pager : jQuery('#tally_pager'),
				sortname : 'dealDate',
				viewrecords : true,
				sortorder : "asc",
				width : 750,
				// autowidth : true,
				height : 300,
				rownumbers : true,
				caption : "收支流水账",
				ondblClickRow : onRowDblClick
			}).navGrid('#tally_pager', {
		edit : false,
		add : false,
		del : false,
		search : false
	});
});// 表格

/**
 * 收入对话框
 */
$(function() {
	var inoutItemType = 1;// 0:支出；1:收入；2:存取款；3:转账

	var id = $("#in_id");
	var dealDate = $("#in_dealDate");
	var inoutItem = $("#in_inoutItem");
	var money = $("#in_money");
	var account = $("#in_account");
	var relationUser = $("#in_relationUser");
	var memo = $("#in_memo");

	var allFields = $( []).add(id).add(dealDate).add(inoutItem).add(money).add(
			account).add(relationUser).add(memo);

	var getPostData = function() {
		return {
			in_id : id.val(),
			in_dealDate : dealDate.val(),
			in_inoutItem : inoutItem.val(),
			in_money : money.val(),
			in_account : account.val(),
			in_relationUser : relationUser.val(),
			in_memo : memo.val()
		};
	};

	// 保存数据，如果isClose为true，则“保存并退出”，否则“保存并继续”
	var saveData = function(isClose) {
		var bValid = true;
		allFields.removeClass('ui-state-error');

		var validator = new Assistant.validator('in_validateTips');
		bValid = bValid && validator.checkValue(dealDate, "", "请输入收入日期");
		bValid = bValid && validator.checkLength(dealDate, "收入日期", 10, 10);
		bValid = bValid && validator.checkValue(inoutItem, "-1", "请选择收入项目");
		bValid = bValid && validator.checkValue(money, "", "请输入收入金额");
		bValid = bValid && validator.checkZero(money, "金额不能为零");
		bValid = bValid && validator.checkMoney(money, "金额非法");
		bValid = bValid && validator.checkValue(account, "-1", "请选择收入账户");
		bValid = bValid && validator.checkValue(relationUser, "-1", "请选择收入人");
		bValid = bValid && validator.checkLength(memo, "备注", 0, 50);

		if (bValid) {
			// 保存数据到后台，同时往grid中新增一条记录
			$.ajax( {
				type : "POST",
				url : webRoot + "/hcm/tally.t?m=save&t=in",
				async : false,// 同步请求
				data : getPostData(),
				dataType : 'json',
				success : function(data, textStatus) {
					var resultData = new ResultData(data);
					var flag = resultData.getUserData("flag");

					var row = {};
					row.id = resultData.getUserData("id");

					row.dealDate = $("#in_dealDate").val();
					row.inoutItemId = $("#in_inoutItem").val();
					row.inoutItemName = Assistant.tools
							.getSelectedText("in_inoutItem");
					row.inoutItemType = inoutItemType;// 收支项目的类型
					row.money = $("#in_money").val();

					row.moneyType = resultData.getUserData("moneyType");

					row.inAccountId = $("#in_account").val();
					row.inAccountName = Assistant.tools
							.getSelectedText("in_account");
					row.relationUserId = $("#in_relationUser").val();
					row.relationUserName = Assistant.tools
							.getSelectedText("in_relationUser");
					row.memo = $("#in_memo").val();

					row.logUserId = resultData.getUserData("logUserId");
					row.logUserName = resultData.getUserData("logUserName");

					if (resultData.isSuccess()) {
						var myGrid = $("#tally_grid");

						if (flag == 'insert') {
							myGrid.addRowData(row.id, row);
						}
						if (flag == 'update') {
							myGrid.setRowData(row.id, row);
						}
					} else {
						alert("保存失败，请重试！");
					}

					$("#in_id").val('-1');
					$("#in_money").val('');
					$("#in_memo").val('');

					if (isClose) {// 保存并退出——关闭对话框
						$("#dialog_in").dialog('close');
					} else {// 保存并继续
					}
				}
			});
		}
	};

	$("#dialog_in").dialog( {
		bgiframe : true,
		autoOpen : false,
		width : 450,
		modal : true,
		resizable : false,
		buttons : {
			'取消' : function() {
				$(this).dialog('close');
			},
			'保存并退出' : function() {
				saveData(true);
			},
			'保存并继续' : function() {
				saveData(false);
			}
		},
		close : function() {
			allFields.val('').removeClass('ui-state-error');
		}
	});

	// 输入按钮点击事件
	$('#btn_in').click(function() {
			$("#in_id").val('-1');
			$("#in_money").val('');
			$("#in_memo").val('');
			$("#in_dealDate").val(Assistant.tools.getNowDate());// 新增收入时，初始化交易日期为“今天”
			$("#in_relationUser").val(Assistant.User.id);// 新增收入时，初始化关联人员为当前用户

			$('#dialog_in').dialog('open');
			$('#dialog_in').dialog('option', 'title', '新增收入');
		}).hover(function() {
		$(this).addClass("ui-state-hover");
	}, function() {
		$(this).removeClass("ui-state-hover");
	}).mousedown(function() {
		$(this).addClass("ui-state-active");
	}).mouseup(function() {
		$(this).removeClass("ui-state-active");
	});
});// 收入对话框

/**
 * 支出对话框
 */
$(function() {
	var inoutItemType = 0;// 0:支出；1:收入；2:存取款；3:转账

	var id = $("#out_id");
	var dealDate = $("#out_dealDate");
	var inoutItem = $("#out_inoutItem");
	var money = $("#out_money");
	var account = $("#out_account");
	var relationUser = $("#out_relationUser");
	var memo = $("#out_memo");

	var allFields = $( []).add(id).add(dealDate).add(inoutItem).add(money).add(
			account).add(relationUser).add(memo);

	var getPostData = function() {
		return {
			out_id : id.val(),
			out_dealDate : dealDate.val(),
			out_inoutItem : inoutItem.val(),
			out_money : money.val(),
			out_account : account.val(),
			out_relationUser : relationUser.val(),
			out_memo : memo.val()
		};
	};

	// 保存数据，如果isClose为true，则“保存并退出”，否则“保存并继续”
	var saveData = function(isClose) {
		var bValid = true;
		allFields.removeClass('ui-state-error');

		var validator = new Assistant.validator('out_validateTips');
		bValid = bValid && validator.checkValue(dealDate, "", "请输入支出日期");
		bValid = bValid && validator.checkLength(dealDate, "支出日期", 10, 10);
		bValid = bValid && validator.checkValue(inoutItem, "-1", "请选择支出项目");
		bValid = bValid && validator.checkValue(money, "", "请输入支出金额");
		bValid = bValid && validator.checkZero(money, "金额不能为零");
		bValid = bValid && validator.checkMoney(money, "金额非法");
		bValid = bValid && validator.checkValue(account, "-1", "请选择支出账户");
		bValid = bValid && validator.checkValue(relationUser, "-1", "请选择支出人");
		bValid = bValid && validator.checkLength(memo, "备注", 0, 50);

		if (bValid) {
			// 保存数据到后台，同时往grid中新增一条记录
			$.ajax( {
				type : "POST",
				url : webRoot + "/hcm/tally.t?m=save&t=out",
				async : false,// 同步请求
				data : getPostData(),
				dataType : 'json',
				success : function(data, textStatus) {
					var resultData = new ResultData(data);
					var flag = resultData.getUserData("flag");

					var row = {};
					row.id = resultData.getUserData("id");

					row.dealDate = $("#out_dealDate").val();
					row.inoutItemId = $("#out_inoutItem").val();
					row.inoutItemName = Assistant.tools
							.getSelectedText("out_inoutItem");
					row.inoutItemType = inoutItemType;// 收支项目的类型
					row.money = $("#out_money").val();

					row.moneyType = resultData.getUserData("moneyType");

					row.outAccountId = $("#out_account").val();
					row.outAccountName = Assistant.tools
							.getSelectedText("out_account");
					row.relationUserId = $("#out_relationUser").val();
					row.relationUserName = Assistant.tools
							.getSelectedText("out_relationUser");
					row.memo = $("#out_memo").val();

					row.logUserId = resultData.getUserData("logUserId");
					row.logUserName = resultData.getUserData("logUserName");

					if (resultData.isSuccess()) {
						var myGrid = $("#tally_grid");

						if (flag == 'insert') {
							myGrid.addRowData(row.id, row);
						}
						if (flag == 'update') {
							myGrid.setRowData(row.id, row);
						}
					} else {
						alert("保存失败，请重试！");
					}

					$("#out_id").val('-1');
					$("#out_money").val('');
					$("#out_memo").val('');

					if (isClose) {// 保存并退出——关闭对话框
						$("#dialog_out").dialog('close');
					} else {// 保存并继续
					}
				}
			});
		}
	};

	$("#dialog_out").dialog( {
		bgiframe : true,
		autoOpen : false,
		width : 450,
		modal : true,
		resizable : false,
		buttons : {
			'取消' : function() {
				$(this).dialog('close');
			},
			'保存并退出' : function() {
				saveData(true);
			},
			'保存并继续' : function() {
				saveData(false);
			}
		},
		close : function() {
			allFields.val('').removeClass('ui-state-error');
		}
	});

	// 输入按钮点击事件
	$('#btn_out').click(function() {
			$("#out_id").val('-1');
			$("#out_money").val('');
			$("#out_memo").val('');
			$("#out_dealDate").val(Assistant.tools.getNowDate());// 新增收入时，初始化交易日期为“今天”
			$("#out_relationUser").val(Assistant.User.id);// 新增支出时，初始化关联人员为当前用户

			$('#dialog_out').dialog('open');
			$('#dialog_out').dialog('option', 'title', '新增支出');
		}).hover(function() {
		$(this).addClass("ui-state-hover");
	}, function() {
		$(this).removeClass("ui-state-hover");
	}).mousedown(function() {
		$(this).addClass("ui-state-active");
	}).mouseup(function() {
		$(this).removeClass("ui-state-active");
	});
});// 支出对话框

/**
 * 存取款对话框
 */
$(function() {
	var inoutItemType = 2;// 0:支出；1:收入；2:存取款；3:转账

	var id = $("#access_id");
	var dealDate = $("#access_dealDate");
	var inoutItem1 = $("#access_inoutItem_1");
	var inoutItem2 = $("#access_inoutItem_2");
	var inoutItem = $("#access_inoutItem");
	var money = $("#access_money");
	var cachAccount = $("#access_cash_account");
	var bankAccount = $("#access_bank_account");
	var relationUser = $("#access_relationUser");
	var memo = $("#access_memo");

	var allFields = $( []).add(id).add(dealDate).add(inoutItem1)
			.add(inoutItem2).add(money).add(cachAccount).add(bankAccount).add(
					relationUser).add(memo);

	var getPostData = function() {
		return {
			access_id : id.val(),
			access_dealDate : dealDate.val(),
			access_inoutItem : inoutItem.val(),// 获取一组radio被选中项的值
			access_money : money.val(),
			access_cash_account : cachAccount.val(),
			access_bank_account : bankAccount.val(),
			access_relationUser : relationUser.val(),
			access_memo : memo.val()
		};
	};

	// 保存数据，如果isClose为true，则“保存并退出”，否则“保存并继续”
	var saveData = function(isClose) {
		var bValid = true;
		allFields.removeClass('ui-state-error');

		var validator = new Assistant.validator('access_validateTips');
		bValid = bValid && validator.checkValue(dealDate, "", "请输入存/取款日期");
		bValid = bValid && validator.checkLength(dealDate, "存/取款日期", 10, 10);
		bValid = bValid && validator.checkValue(money, "", "请输入存取金额");
		bValid = bValid && validator.checkZero(money, "金额不能为零");
		bValid = bValid && validator.checkMoney(money, "金额非法");
		bValid = bValid && validator.checkValue(cachAccount, "-1", "请选择现金账户");
		bValid = bValid && validator.checkValue(bankAccount, "-1", "请选择银行账户");
		bValid = bValid && validator.checkValue(relationUser, "-1", "请选择存/取款人");
		bValid = bValid && validator.checkLength(memo, "备注", 0, 50);

		if (bValid) {
			// 保存数据到后台，同时往grid中新增一条记录
			$.ajax( {
				type : "POST",
				url : webRoot + "/hcm/tally.t?m=save&t=access",
				async : false,// 同步请求
				data : getPostData(),
				dataType : 'json',
				success : function(data, textStatus) {
					var resultData = new ResultData(data);
					var flag = resultData.getUserData("flag");

					var row = {};
					row.id = resultData.getUserData("id");

					row.dealDate = $("#access_dealDate").val();
					row.inoutItemId = $("#access_inoutItem").val();// 获取一组radio被选中项的值
					if (row.inoutItemId == '1') {
						row.inoutItemName = '取款';
					} else if (row.inoutItemId == '2') {
						row.inoutItemName = '存款';
					}
					row.inoutItemType = inoutItemType;// 收支项目的类型
					row.money = $("#access_money").val();

					row.moneyType = resultData.getUserData("moneyType");

					if (row.inoutItemId == '1') {// 取款
						row.inAccountId = $("#access_cash_account").val();
						row.inAccountName = Assistant.tools
								.getSelectedText("access_cash_account");
						row.outAccountId = $("#access_bank_account").val();
						row.outAccountName = Assistant.tools
								.getSelectedText("access_bank_account");
					} else if (row.inoutItemId == '2') {// 存款
						row.inAccountId = $("#access_bank_account").val();
						row.inAccountName = Assistant.tools
								.getSelectedText("access_bank_account");
						row.outAccountId = $("#access_cash_account").val();
						row.outAccountName = Assistant.tools
								.getSelectedText("access_cash_account");
					}

					row.relationUserId = $("#access_relationUser").val();
					row.relationUserName = Assistant.tools
							.getSelectedText("access_relationUser");
					row.memo = $("#access_memo").val();

					row.logUserId = resultData.getUserData("logUserId");
					row.logUserName = resultData.getUserData("logUserName");

					if (resultData.isSuccess()) {
						var myGrid = $("#tally_grid");

						if (flag == 'insert') {
							myGrid.addRowData(row.id, row);
						}
						if (flag == 'update') {
							myGrid.setRowData(row.id, row);
						}
					} else {
						alert("保存失败，请重试！");
					}

					$("#access_id").val('-1');
					$("#access_money").val('');
					$("#access_memo").val('');

					if (isClose) {// 保存并退出——关闭对话框
						$("#dialog_access").dialog('close');
					} else {// 保存并继续
					}
				}
			});
		}
	};

	$("#dialog_access").dialog( {
		bgiframe : true,
		autoOpen : false,
		width : 450,
		modal : true,
		resizable : false,
		buttons : {
			'取消' : function() {
				$(this).dialog('close');
			},
			'保存并退出' : function() {
				saveData(true);
			},
			'保存并继续' : function() {
				saveData(false);
			}
		},
		close : function() {
			allFields.val('').removeClass('ui-state-error');
		}
	});

	// 输入按钮点击事件
	$('#btn_access').click(function() {
			$("#access_id").val('-1');
			$("#access_money").val('');
			$("#access_memo").val('');
			$("#access_dealDate").val(Assistant.tools.getNowDate());// 新增收入时，初始化交易日期为“今天”
			$("#access_relationUser").val(Assistant.User.id);// 新增支出时，初始化关联人员为当前用户

			$('#dialog_access').dialog('open');
			$('#dialog_access').dialog('option', 'title', '新增存取款');
		}).hover(function() {
		$(this).addClass("ui-state-hover");
	}, function() {
		$(this).removeClass("ui-state-hover");
	}).mousedown(function() {
		$(this).addClass("ui-state-active");
	}).mouseup(function() {
		$(this).removeClass("ui-state-active");
	});
});// 存取款对话框

/**
 * 转帐对话框
 */
$(function() {
	var inoutItemType = 3;// 0:支出；1:收入；2:存取款；3:转账

	var id = $("#transfer_id");
	var dealDate = $("#transfer_dealDate");
	var inoutItem = $("#transfer_inoutItem");
	var money = $("#transfer_money");
	var outAccount = $("#transfer_out_account");
	var inAccount = $("#transfer_in_account");
	var relationUser = $("#transfer_relationUser");
	var memo = $("#transfer_memo");

	var allFields = $( []).add(id).add(dealDate).add(inoutItem)
			.add(money).add(outAccount).add(inAccount).add(
					relationUser).add(memo);

	var getPostData = function() {
		return {
			transfer_id : id.val(),
			transfer_dealDate : dealDate.val(),
			transfer_inoutItem : inoutItem.val(),// 获取一组radio被选中项的值
			transfer_money : money.val(),
			transfer_out_account : outAccount.val(),
			transfer_in_account : inAccount.val(),
			transfer_relationUser : relationUser.val(),
			transfer_memo : memo.val()
		};
	};

	// 保存数据，如果isClose为true，则“保存并退出”，否则“保存并继续”
	var saveData = function(isClose) {
		var bValid = true;
		allFields.removeClass('ui-state-error');

		var validator = new Assistant.validator('transfer_validateTips');
		bValid = bValid && validator.checkValue(dealDate, "", "请输入转账日期");
		bValid = bValid && validator.checkLength(dealDate, "转账日期", 10, 10);
		bValid = bValid && validator.checkValue(money, "", "请输入转账金额");
		bValid = bValid && validator.checkZero(money, "金额不能为零");
		bValid = bValid && validator.checkMoney(money, "金额非法");
		bValid = bValid && validator.checkValue(outAccount, "-1", "请选择转出账户");
		bValid = bValid && validator.checkValue(inAccount, "-1", "请选择转入账户");
		bValid = bValid && validator.checkValue(relationUser, "-1", "请选择转账人");
		bValid = bValid && validator.checkLength(memo, "备注", 0, 50);

		if (bValid) {
			// 保存数据到后台，同时往grid中新增一条记录
			$.ajax( {
				type : "POST",
				url : webRoot + "/hcm/tally.t?m=save&t=transfer",
				async : false,// 同步请求
				data : getPostData(),
				dataType : 'json',
				success : function(data, textStatus) {
					var resultData = new ResultData(data);
					var flag = resultData.getUserData("flag");

					var row = {};
					row.id = resultData.getUserData("id");

					row.dealDate = $("#transfer_dealDate").val();
					row.inoutItemId = $("#transfer_inoutItem").val();// 获取一组radio被选中项的值
					row.inoutItemName = '转账';
					row.inoutItemType = inoutItemType;// 收支项目的类型
					row.money = $("#transfer_money").val();

					row.moneyType = resultData.getUserData("moneyType");

					row.inAccountId = $("#transfer_in_account").val();
					row.inAccountName = Assistant.tools.getSelectedText("transfer_in_account");
					row.outAccountId = $("#transfer_out_account").val();
					row.outAccountName = Assistant.tools.getSelectedText("transfer_out_account");

					row.relationUserId = $("#transfer_relationUser").val();
					row.relationUserName = Assistant.tools.getSelectedText("transfer_relationUser");
					row.memo = $("#transfer_memo").val();

					row.logUserId = resultData.getUserData("logUserId");
					row.logUserName = resultData.getUserData("logUserName");

					if (resultData.isSuccess()) {
						var myGrid = $("#tally_grid");

						if (flag == 'insert') {
							myGrid.addRowData(row.id, row);
						}
						if (flag == 'update') {
							myGrid.setRowData(row.id, row);
						}
					} else {
						alert("保存失败，请重试！");
					}

					$("#transfer_id").val('-1');
					$("#transfer_money").val('');
					$("#transfer_memo").val('');

					if (isClose) {// 保存并退出——关闭对话框
						$("#dialog_transfer").dialog('close');
					} else {// 保存并继续
					}
				}
			});
		}
	};

	$("#dialog_transfer").dialog( {
		bgiframe : true,
		autoOpen : false,
		width : 450,
		modal : true,
		resizable : false,
		buttons : {
			'取消' : function() {
				$(this).dialog('close');
			},
			'保存并退出' : function() {
				saveData(true);
			},
			'保存并继续' : function() {
				saveData(false);
			}
		},
		close : function() {
			allFields.val('').removeClass('ui-state-error');
		}
	});

	// 输入按钮点击事件
	$('#btn_transfer').click(function() {
			$("#transfer_id").val('-1');
			$("#transfer_money").val('');
			$("#transfer_memo").val('');
			$("#transfer_dealDate").val(Assistant.tools.getNowDate());// 新增收入时，初始化交易日期为“今天”
			$("#transfer_relationUser").val(Assistant.User.id);// 新增支出时，初始化关联人员为当前用户
	
			$('#dialog_transfer').dialog('open');
			$('#dialog_transfer').dialog('option', 'title', '新增转账');
		}).hover(function() {
		$(this).addClass("ui-state-hover");
	}, function() {
		$(this).removeClass("ui-state-hover");
	}).mousedown(function() {
		$(this).addClass("ui-state-active");
	}).mouseup(function() {
		$(this).removeClass("ui-state-active");
	});
});// 转帐对话框
